<template>
  <div class="content">
    <div class="title">
      <div class="text">
        <div class="font">当前设备在线率</div>
      </div>
    </div>
    <div class="echart">
      <div id="inLine" style="width:100%;height:300px;" />
    </div>
  </div>
</template>

<script>
import { currentData } from '@/api/autonym'

export default {
  name: 'InLine',
  data() {
    return {
      data: {
        sign: 0,
        total: 0
      }
    }
  },
  mounted() {
    // this.getDetail()
  },
  methods: {
    // 获取数据
    async getDetail() {
      const res = await currentData()
      if (res.code === 200) {
        if (res.data) {
          this.data = res.data
        }
        this.$nextTick(function() {
          this.drawPie()
        })
      }
    },
    drawPie() {
      const myChart = this.$echarts.init(document.getElementById('inLine'))
      var m2R2Data = [
        { value: 335, legendname: '在线设备', name: '在线设备  335', itemStyle: { color: '#36CBCB' }},
        { value: 310, legendname: '离线设备', name: '离线设备  310', itemStyle: { color: '#D29A00' }}
      ]
      const option = {
        title: [
          // {
          //   text: '当前设备在线率',
          //   textStyle: {
          //     fontSize: 16,
          //     color: '#ffffff'
          //   },
          //   left: '2%'
          // },
          {
            text: '合计',
            subtext: 12312 + '个',
            textStyle: {
              fontSize: 20,
              color: '#ffffff'
            },
            subtextStyle: {
              fontSize: 20,
              color: '#ffffff'
            },
            textAlign: 'center',
            x: '34.5%',
            y: '44%'
          }],
        tooltip: {
          trigger: 'item',
          formatter: function(parms) {
            var str = parms.seriesName + '</br>' +
            parms.marker + '' + parms.data.legendname + '</br>' +
            '数量：' + parms.data.value + '</br>' +
            '占比：' + parms.percent + '%'
            return str
          }
        },
        legend: {
          type: 'scroll',
          orient: 'vertical',
          left: '70%',
          align: 'left',
          top: 'middle',
          textStyle: {
            color: '#8C8C8C'
          },
          height: 250
        },
        series: [
          {
            name: '设备：',
            type: 'pie',
            center: ['35%', '50%'],
            radius: ['40%', '65%'],
            clockwise: false, // 饼图的扇区是否是顺时针排布
            avoidLabelOverlap: false,
            label: {
              normal: {
                show: true,
                position: 'outter',
                borderColor: '#25262F',
                borderWidth: 6,
                formatter: function(parms) {
                  return parms.data.legendname
                }
              }
            },
            labelLine: {
              normal: {
                length: 5,
                length2: 3,
                smooth: true
              }
            },
            data: m2R2Data
          }
        ]
      }
      myChart.setOption(option, window.onresize = myChart.resize)
      setTimeout(function() {
        myChart.on('mouseover', function(params) {
          if (params.name == m2R2Data[0].legendname) {
            myChart.dispatchAction({
              type: 'highlight',
              seriesIndex: 0,
              dataIndex: 0
            })
          } else {
            myChart.dispatchAction({
              type: 'downplay',
              seriesIndex: 0,
              dataIndex: 0
            })
          }
        })

        myChart.on('mouseout', function(params) {
          myChart.dispatchAction({
            type: 'highlight',
            seriesIndex: 0,
            dataIndex: 0
          })
        })
        myChart.dispatchAction({
          type: 'highlight',
          seriesIndex: 0,
          dataIndex: 0
        })
      }, 1000)
    }
  }
}
</script>

<style lang="scss" scoped>
.content{
  // color: #ffffff;
  // border: 1px solid rgba(221,221,221,0.2);
  margin: 0;
  padding: 10px;
  .title{
    display: flex;
    .text{
      flex: 1;
      text-align: left;
      .font{
        color:#ffffff;
        cursor: pointer;
        font-size: 12px;
        font-weight: 400;
        padding: 5px 0;
        .number{
          font-size:14px;
          font-weight:500;
          color:rgba(255,255,255,0.85);
        }
      }
      .money{
         font-size: 12px;
        font-weight: 400;
        color: #ffffff;
      }
    }
  }
  .echart{
    // height: 70px;
    margin: 0 0 32px 0;
  }
  .name{
    margin-top: -60px;
    text-align: center;
    color:rgba(255,255,255,1);
    .text{
      text-align: center;
      margin-bottom: 6px;
      .bground{
        width:8px;
        height:8px;
        background:rgba(210,154,0,1);
        border-radius: 4px;
        float: left;
        margin-left: 27%;
        margin-top: 5px;
      }
    }
  }
}
</style>
